﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

namespace Sgr.Utilities
{
    /// <summary>
    /// 硬件环境工具类
    /// </summary>
    public static class HardwareEnvHelper
    {
        /// <summary>
        /// 获取活动的网络接口
        /// </summary>
        private static IEnumerable<NetworkInterface> GetActiveNetworkInterfaces()
        {
            return NetworkInterface
                .GetAllNetworkInterfaces()
                .Where(network =>
                    network.OperationalStatus == OperationalStatus.Up &&
                    network.NetworkInterfaceType != NetworkInterfaceType.Loopback);
        }

        /// <summary>
        /// 获取当前计算机设备的IP地址
        /// </summary>
        /// <returns>IP地址列表</returns>
        public static IEnumerable<string> GetHostServerIps()
        {
            try
            {
                return GetActiveNetworkInterfaces()
                    .Select(network => network.GetIPProperties())
                    .OrderByDescending(properties => properties.GatewayAddresses.Count)
                    .SelectMany(properties => properties.UnicastAddresses)
                    .Where(address =>
                        !IPAddress.IsLoopback(address.Address) &&
                        address.Address.AddressFamily == AddressFamily.InterNetwork)
                    .Select(address => address.Address.ToString());
            }
            catch (NetworkInformationException ex)
            {
                throw new InvalidOperationException("获取IP地址时发生错误", ex);
            }
        }

        /// <summary>
        /// 获取当前计算机设备的Mac地址
        /// </summary>
        /// <param name="separator">MAC地址分隔符</param>
        /// <returns>MAC地址列表</returns>
        /// <exception cref="ArgumentNullException">当separator为null时抛出</exception>
        public static IEnumerable<string> GetHostServerMacs(string separator = "-")
        {
            if (separator == null)
                throw new ArgumentNullException(nameof(separator));

            try
            {
                return GetActiveNetworkInterfaces()
                    .Where(adapter =>
                        adapter.GetIPProperties()
                            .UnicastAddresses
                            .Any(addr => addr.Address.AddressFamily == AddressFamily.InterNetwork))
                    .Select(adapter =>
                    {
                        byte[] bytes = adapter.GetPhysicalAddress().GetAddressBytes();
                        return string.Join(separator, bytes.Select(b => b.ToString("X2")));
                    });
            }
            catch (NetworkInformationException ex)
            {
                throw new InvalidOperationException("获取MAC地址时发生错误", ex);
            }
        }

        /// <summary>
        /// 获取当前计算机名称
        /// </summary>
        /// <returns>计算机名称</returns>
        public static string GetMachineName()
        {
            return Environment.MachineName;
        }
    }
}